C++:round函数用法 |
您所在的位置:网站首页 › c语言 round › C++:round函数用法 |
平时刷题时经常会碰见要求四舍五入的情况,此时用round()函数最方便了 正常用法:对于小数而言,round()函数仅仅保留到整数位,即仅仅对小数点后一位四舍五入, 样例: round(1.5)=2.000000 round(1.56)=2.000000 round(-1.5)=-2.000000 round(-1.56)=-2.000000 保留小数用法:如果想要保留小数位数,则可以先乘后除 样例: #include #include int main() { double x=1.5684; printf("对1.5684保留两位有效数字:"); printf("%.2lf\n",round(x*100)/100); return 0; } 结果: 对1.5684保留两位有效数字:1.57 手写round():round()函数原理为 x=(int)(x+0.5)公式,故可以自己写出round()函数 #include double round(double x) { return (int)(x+0.5); } |
今日新闻 |
推荐新闻 |
CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3 |